package cn.itsource.upload.controller;


import cn.hutool.core.io.file.FileNameUtil;
import cn.itsource.upload.service.MinioIService;
import cn.itsource.utils.JsonResult;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


@RestController
@RequestMapping("/minio")
public class MinioController {

    @Autowired
    private MinioIService minioIService;

    @PostMapping("/upload")
    public JsonResult upload(@RequestParam("file") MultipartFile file) throws IOException{
        try {
            String filePath =minioIService.upload(file);
            return JsonResult.success(filePath);
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }





    /*@DeleteMapping
    public void delete(String fileName) throws ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        //判断桶是否存在
        boolean res=minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (res) {
            //删除文件
            minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName)
                    .object(fileName).build());
        }
    }*/

    /*@GetMapping("/download")
    public ResponseEntity<byte[]> download(@RequestParam("file") String fileName) {
        try {
            byte[] data = minioClient.getObject("java0926", fileName).readAllBytes();
            return ResponseEntity.ok().body(data);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("下载失败");
        }
    }*/
}
